using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{
    public partial class VillageLookup : UserControl
    {
        public VillageLookup()
        {
            InitializeComponent();
        }
        private const string MSG = "Please enter a 3 digit number for both x and y village cordinates.";
        private Village selectedVillage;
        public Village SelectedVillage
        {
            get
            {
                return selectedVillage;
            }
            set
            {
                selectedVillage = value;
                villageDisplay1.VillageToDisplay = value;
                if (SelectedVillageChangeFoundEvent != null)
                {
                    SelectedVillageChangeFoundEvent(value);
                }
            }
        }

        private void txtXorY_TextChanged(object sender, EventArgs e)
        {
            selectedVillage = null;
            if (txtX.Text.Length == 3 && txtY.Text.Length == 3)
            {
                int x, y;
                if (System.Int32.TryParse(txtY.Text, out y) && System.Int32.TryParse(txtX.Text, out x))
                {
                    Village vil = Worlds.currentWorld.Village(x, y);
                    SelectedVillage = vil;
                    errorProvider1.SetError(txtY, "");
                    
                    if (vil == null)
//                    {
  //                      villageDisplay1.VillageToDisplay = vil;
//                        lblFoundVillage.Text = App.FormatVillageForDisplay(vil);
    //                }
      //              else
                    {
                        //villageDisplay1.VillageToDisplay = vil;
                        //lblFoundVillage.Text = "no village at these cordinates.\nSelect different cordinates";
                        errorProvider1.SetError(txtY, "no village at these cordinates.\nSelect different cordinates");
                    }
                }
                else
                {
                    SelectedVillage = null;
                    errorProvider1.SetError(txtY, MSG);
                }
            }
            else
            {
                SelectedVillage = null;
            }
        }

        public delegate void SelectedVillageChangeDelegate(Village vil);

        public event SelectedVillageChangeDelegate SelectedVillageChangeFoundEvent;

        private void VillageLookup_Load(object sender, EventArgs e)
        {
            
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void txtPlayer_Leave(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtPlayer, "");
            cbVillages.Items.Clear();
            if (txtPlayer.Text.Trim() != string.Empty)
            {
                Player player = Worlds.currentWorld.Player(txtPlayer.Text.Trim());

                if (player == null)
                {
                    errorProvider1.SetError(txtPlayer, "could not find player '" + txtPlayer.Text.Trim() + "'");
                }
                else
                {
                    //SortedList<string, Village> villages = player.Villages;
                    List<Village> villages = player.Villages;

                    foreach(Village vil in villages) {
                        cbVillages.Items.Add(vil);                   
                    }


                    
                }
            }
        }

        private void txtPlayer_TextChanged(object sender, EventArgs e)
        {
            errorProvider1.SetError(txtPlayer, "");
            cbVillages.Items.Clear();
        }

        private void cbVillages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbVillages.SelectedItem != null)
            {
                SelectedVillage = (Village)cbVillages.SelectedItem;
            }
        }

    }
}
